<?php

class NCore_SiteTest {
    private $markup = NULL;

    public function setMarkup($markup){
        $this->markup = $markup;
    }
    public function getMakrup(){
        return $this->markup;
    }
    public function process(){
        $url = $this->getURL();
        $path = $this->getTestPath();
        $pages = $this->getPages($path);
        $results = '<ul>';

        foreach($pages as $page){
            $doc = new DOMDocument();
            $pageURL = $url.$page;
            $doc->validateOnParse = true;
            $doc->load($pageURL);
            $count = $doc->getElementById('failed_tests')->nodeValue;
            $results .= "<li><strong><a href='$pageURL'>$pageURL</a> ".(($count > 0)?"<span style=\"color:#ff0000;\">$count test failures</span>":"<span style=\"color:#00ff00;\">Passed</span>")."</strong></li>";
        }

        $results .= "</ul>";

        $template = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Nephtali Site Test Results</title>
{$this->getStyleSheet()}
</head>

<body>
<div id="container">
<h1>Nephtali Tests</h1>
<p>Below, you can view the results of all page tests set up within this website.</p>
{$results}
</div>
</body>
</html>
EOD;
        return $template;
    }
    protected function getURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]; // could append with 'REQUEST_URI' to add file name
        } else {
        $pageURL .= $_SERVER["SERVER_NAME"];// could append with 'REQUEST_URI' to add file name
        }

        return $pageURL;
    }
    protected function getTestPath(){
        $inc_paths = explode(PATH_SEPARATOR, get_include_path());

        foreach($inc_paths as $path)
        {
            $trialpath = $path.'/NTest';

            if(is_dir($trialpath))
            {
                return $trialpath;
            }
        }

        throw new Exception('We could not determine the path for the NTest directory');
    }
    protected function getPages($path){
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
        $pages = array();

        foreach($iterator as $entry)
        {
            $pieces = explode('NTest', $entry);
            $pages[] = $pieces[1];
        }
        return $pages;
    }
    protected function getStyleSheet(){
        return $styles = <<<EOD
    <style type="text/css">
        html,body{background-color:#EEEEEE; font-size:small;}
        h1{150%; padding-top:10px;}
        h2{140%;}
        h3{130%;}
        #container{width:760px; margin:0 auto; background-color:#ffffff; border:1px solid #dddddd;}
        #container h1, #container h2, #container h3, #container h4, #container p, #container ul, #container dl, #container ol, #container form{margin-left:5px; margin-right:5px;}
        ul{padding:0;}
        li{padding:3px; margin:3px; list-style:none;}
        li.passed{background-color:#9C9;}
        li.failed{background-color:#C99;}
        dt{font-weight:bold; margin-top:10px;}
        dd{margin-left:20px; padding-left:0;}
    </style>
EOD;

    }
}

?>